/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月20日         Root.Lu
 */

package com.coocaa.launcher.framework.manager.configdata.data;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.RemoteException;

import com.coocaa.x.framework.data.JObject;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litepm.data.AppInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class LauncherAppItemTableData extends AbstractData {
    public static enum APP_ITEM_TYPE {
        APP_FOLDER_TYPE(1 << 3),
        APP_WIDGET_TYPE(1 << 2),
        APP_SHORTCUT_TYPE(1 << 1),
        APP_ENTRY_TYPE(1);

        public int flag;

        private APP_ITEM_TYPE(int flag) {
            this.flag = flag;
        }
    }

    ;

    public static abstract class AppItemData extends JObject implements Cloneable {
        protected String id = UUID.randomUUID().toString();
        protected String layoutExtra = null;
        protected String type = APP_ITEM_TYPE.APP_ENTRY_TYPE.toString();
        protected String title = null;
        protected String packageName = null;
        protected String mainActivity = null;
        protected boolean isSystem = false;

        //不可见，隐藏  false:可见  true:不可见
        protected boolean invisible = false;

        public boolean isInvisible() {
            return invisible;
        }

        public void setInvisible(boolean invisible) {
            this.invisible = invisible;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getLayoutExtra() {
            return layoutExtra;
        }

        public void setLayoutExtra(String layoutExtra) {
            this.layoutExtra = layoutExtra;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public boolean isSystem() {
            return isSystem;
        }

        public void setSystem(boolean isSystem) {
            this.isSystem = isSystem;
        }

        @Override
        public boolean equals(Object o) {
            // TODO Auto-generated method stub
            try {
                return id.equals(((AppItemData) o).id);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public String getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }

        public String getMainActivity() {
            return mainActivity;
        }

        public void setMainActivity(String mainActivity) {
            this.mainActivity = mainActivity;
        }

        public AppItemData clone() throws CloneNotSupportedException {
            // TODO Auto-generated method stub
            return (AppItemData) super.clone();
        }
    }

    public static abstract class FoldableAppData extends AppItemData {
    }

    public static class AppFolderData extends AppItemData {
        private List<FoldableAppData> childs = new ArrayList<FoldableAppData>();

        public AppFolderData() {
            this.type = APP_ITEM_TYPE.APP_FOLDER_TYPE.toString();
        }

        public void addFoldableAppItem(FoldableAppData data) {
            synchronized (childs) {
                childs.add(data);
            }
        }

        public void removeFoldableAppItem(FoldableAppData data) {
            synchronized (childs) {
                childs.remove(data);
            }
        }

        public List<FoldableAppData> getFoldableChilds() {
            List<FoldableAppData> ret = new ArrayList<FoldableAppData>();
            synchronized (childs) {
                for (FoldableAppData data : childs)
                    ret.add(data);
            }
            return ret;
        }

        public boolean containsChild(FoldableAppData data) {
            synchronized (childs) {
                for (FoldableAppData c : childs) {
                    if (c.equals(data.getId()))
                        return true;
                }
            }
            return false;
        }
    }

    public static class AppEntryData extends FoldableAppData {
        public static AppEntryData create(String packageName) {
            try {
                AppInfo info = XServiceManager.getLitePMService().getAppInfo(packageName);
                if (info != null) {
                    AppEntryData data = new AppEntryData();
                    data.setTitle(info.appName);
                    data.setPackageName(packageName);
                    data.setActivityName(info.mainActivity);
                    return data;
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        private String activityName = null;
        private String iconUrl = null;
        private String localIconPath = null;

        public AppEntryData() {
            this.type = APP_ITEM_TYPE.APP_ENTRY_TYPE.toString();
        }

        public String getActivityName() {
            if (activityName == null || activityName.equals(""))
                return super.getMainActivity();
            return activityName;
        }

        public void setActivityName(String activityName) {
            this.activityName = activityName;
            super.setMainActivity(activityName);
        }

        public String getIconUrl() {
            return iconUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.iconUrl = iconUrl;
        }

        public String getLocalPath() {
            return localIconPath;
        }

        public void setLocalPath(String localPath) {
            this.localIconPath = localPath;
        }

    }

    public static class AppShortCutData extends FoldableAppData {
        private String intent = null;
        private String shortcutIcon = null;
        private boolean duplicate = false;

        public AppShortCutData() {
            this.type = APP_ITEM_TYPE.APP_SHORTCUT_TYPE.toString();
        }

        public String getIntent() {
            return intent;
        }

        public void setIntent(String intent) {
            this.intent = intent;
        }

        public void setShortcutIcon(String shortcutIcon) {
            this.shortcutIcon = shortcutIcon;
        }

        public boolean isDuplicate() {
            return duplicate;
        }

        public void setDuplicate(boolean duplicate) {
            this.duplicate = duplicate;
        }

        public String getShortcutIcon() {
            return shortcutIcon;
        }

        public Drawable getShortcutIconDrawable() {
            if (shortcutIcon != null) {
                Drawable ret = BitmapDrawable.createFromPath(shortcutIcon);
                return ret;
            }
            return null;
        }
    }

    public static class AppWidgetData extends AppItemData {
        public AppWidgetData() {
            this.type = APP_ITEM_TYPE.APP_WIDGET_TYPE.toString();
        }
    }

    private String dataid = null;
    private String type = null;
    // 模式字段，为了支持以后可能会出现的多种模式，如：老人模式、儿童模式。不同模式之间的数据是相互独立的
    private String pattern = null;
    private String json = null;

    public String getDataid() {
        return dataid;
    }

    public void setDataid(String dataid) {
        this.dataid = dataid;
    }

    public APP_ITEM_TYPE getEnumType() {
        return APP_ITEM_TYPE.valueOf(type);
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public void setAppItem(AppItemData item) {
        this.dataid = item.getId();
        this.json = item.toString();
    }
}
